package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigDecimal;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.IdleAffiliateCpsIncomeDetailMapper;
import com.ruoyi.system.domain.IdleAffiliateCpsIncomeDetail;
import com.ruoyi.system.service.IIdleAffiliateCpsIncomeDetailService;

/**
 * 闲鱼联盟CPS佣金明细Service业务层处理
 * 
 * @author ruoyi
 */
@Service
public class IdleAffiliateCpsIncomeDetailServiceImpl implements IIdleAffiliateCpsIncomeDetailService 
{
    @Autowired
    private IdleAffiliateCpsIncomeDetailMapper idleAffiliateCpsIncomeDetailMapper;

    /**
     * 查询闲鱼联盟CPS佣金明细
     * 
     * @param id 闲鱼联盟CPS佣金明细主键
     * @return 闲鱼联盟CPS佣金明细
     */
    @Override
    public IdleAffiliateCpsIncomeDetail selectIdleAffiliateCpsIncomeDetailById(Long id)
    {
        return idleAffiliateCpsIncomeDetailMapper.selectIdleAffiliateCpsIncomeDetailById(id);
    }

    /**
     * 查询闲鱼联盟CPS佣金明细列表
     * 
     * @param idleAffiliateCpsIncomeDetail 闲鱼联盟CPS佣金明细
     * @return 闲鱼联盟CPS佣金明细
     */
    @Override
    public List<IdleAffiliateCpsIncomeDetail> selectIdleAffiliateCpsIncomeDetailList(IdleAffiliateCpsIncomeDetail idleAffiliateCpsIncomeDetail)
    {
        return idleAffiliateCpsIncomeDetailMapper.selectIdleAffiliateCpsIncomeDetailList(idleAffiliateCpsIncomeDetail);
    }

    /**
     * 新增闲鱼联盟CPS佣金明细
     * 
     * @param idleAffiliateCpsIncomeDetail 闲鱼联盟CPS佣金明细
     * @return 结果
     */
    @Override
    public int insertIdleAffiliateCpsIncomeDetail(IdleAffiliateCpsIncomeDetail idleAffiliateCpsIncomeDetail)
    {
        return idleAffiliateCpsIncomeDetailMapper.insertIdleAffiliateCpsIncomeDetail(idleAffiliateCpsIncomeDetail);
    }

    /**
     * 修改闲鱼联盟CPS佣金明细
     * 
     * @param idleAffiliateCpsIncomeDetail 闲鱼联盟CPS佣金明细
     * @return 结果
     */
    @Override
    public int updateIdleAffiliateCpsIncomeDetail(IdleAffiliateCpsIncomeDetail idleAffiliateCpsIncomeDetail)
    {
        return idleAffiliateCpsIncomeDetailMapper.updateIdleAffiliateCpsIncomeDetail(idleAffiliateCpsIncomeDetail);
    }

    /**
     * 批量删除闲鱼联盟CPS佣金明细
     * 
     * @param ids 需要删除的闲鱼联盟CPS佣金明细主键
     * @return 结果
     */
    @Override
    public int deleteIdleAffiliateCpsIncomeDetailByIds(Long[] ids)
    {
        return idleAffiliateCpsIncomeDetailMapper.deleteIdleAffiliateCpsIncomeDetailByIds(ids);
    }

    /**
     * 删除闲鱼联盟CPS佣金明细信息
     * 
     * @param id 闲鱼联盟CPS佣金明细主键
     * @return 结果
     */
    @Override
    public int deleteIdleAffiliateCpsIncomeDetailById(Long id)
    {
        return idleAffiliateCpsIncomeDetailMapper.deleteIdleAffiliateCpsIncomeDetailById(id);
    }

    /**
     * 根据账单ID查询闲鱼联盟CPS佣金明细
     * 
     * @param billId 账单ID
     * @return 闲鱼联盟CPS佣金明细
     */
    @Override
    public IdleAffiliateCpsIncomeDetail selectIdleAffiliateCpsIncomeDetailByBillId(String billId)
    {
        return idleAffiliateCpsIncomeDetailMapper.selectIdleAffiliateCpsIncomeDetailByBillId(billId);
    }

    /**
     * 根据订单ID查询闲鱼联盟CPS佣金明细
     * 
     * @param orderId 订单ID
     * @return 闲鱼联盟CPS佣金明细
     */
    @Override
    public IdleAffiliateCpsIncomeDetail selectIdleAffiliateCpsIncomeDetailByOrderId(String orderId)
    {
        return idleAffiliateCpsIncomeDetailMapper.selectIdleAffiliateCpsIncomeDetailByOrderId(orderId);
    }

    /**
     * 获取CPS收入明细的统计信息
     * 
     * @return 统计信息，包含总金额、已结算金额、未结算金额和差值
     */
    @Override
    public Map<String, Object> getCpsIncomeStatistics()
    {
        Map<String, Object> result = new HashMap<>();
        
        // 获取所有记录
        List<IdleAffiliateCpsIncomeDetail> allRecords = idleAffiliateCpsIncomeDetailMapper.selectIdleAffiliateCpsIncomeDetailList(new IdleAffiliateCpsIncomeDetail());
        
        // 计算总金额
        BigDecimal totalAmount = BigDecimal.ZERO;
        // 计算已发放金额（billState=2的记录）
        BigDecimal settledAmount = BigDecimal.ZERO;
        
        for (IdleAffiliateCpsIncomeDetail record : allRecords) {
            if (record.getAssessAmount() != null) {

                totalAmount = totalAmount.add(record.getAssessAmount());
                // 只统计状态为待发放(1)或已发放(2)的记录
                if (StringUtils.isNotEmpty(record.getSubPublisherId()) ) {
                    settledAmount = settledAmount.add(record.getAssessAmount());
                }
            }
        }
        
        // 计算未发放金额
        BigDecimal unsettledAmount = totalAmount.subtract(settledAmount);
        
        result.put("totalAmount", totalAmount);
        result.put("settledAmount", settledAmount);
        result.put("unsettledAmount", unsettledAmount);
        result.put("difference", unsettledAmount); // 差值就是未发放金额
        
        return result;
    }
} 